package com.example.gestureactivity.utils;

import java.io.File;

import android.annotation.SuppressLint;
import android.content.pm.ApplicationInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * 用于对WebView进行设置的工具类
 * 
 * @author chenchong
 * 
 */
public class WebViewUtils {

	@SuppressWarnings("unused")
	private static final String	TAG	= "WebViewUtils";

	/**
	 * 设置WebView中字体的缩放值
	 * 
	 * @param webView
	 * @param fontSize
	 */
	public static void setFontSize(WebView webView, int fontSize) {

		int sdkVersion = android.os.Build.VERSION.SDK_INT; // 当前使用的SDK版本
		WebSettings settings = webView.getSettings();

		setFontSize(settings, sdkVersion, fontSize);
	}

	private static void setFontSize(WebSettings settings, int sdkVersion,
			int fontSize) {

		if (fontSize <= 1) {
			fontSize = 1;
		} else if (fontSize >= 5) {
			fontSize = 5;
		} else {
		}

		if (sdkVersion < 14) {
			setTextSize(settings, fontSize);
		} else {
			setZoom(settings, fontSize);
		}
	}

	/**
	 * 清理当前应用WebView的缓存
	 * @param info
	 */
	public static void clearCache(ApplicationInfo info) {

		String dataDir = info.dataDir; // 应用的缓存目录

		String cacheDir = dataDir + "/cache/webviewCache";//缓存文件目录
		String cacheDbDir = dataDir + "/databases";//缓存数据库目录

		File cacheFiles = new File(cacheDir);
		File cacheDbFiles = new File(cacheDbDir);

		if (null != cacheFiles && cacheFiles.exists()
				&& cacheFiles.isDirectory()) {
			File[] listFiles = cacheFiles.listFiles();
			for (int i = 0; i < listFiles.length; i++) {
				listFiles[i].delete();
			}
		}
		if (null != cacheDbFiles && cacheDbFiles.exists()
				&& cacheDbFiles.isDirectory()) {
			File[] listFiles = cacheDbFiles.listFiles();
			for (int i = 0; i < listFiles.length; i++) {
				String dbName = listFiles[i].getName();
				if (null != dbName
						&& ("webview.db".equals(dbName) || "webviewCache.db"
								.equals(dbName))) {
					listFiles[i].delete();
				}
			}
		}

	}

	/*****************************************************************************************/
	@Deprecated
	private static void setTextSize(WebSettings settings, int fontSize) {

		switch (fontSize) {
		case 1:
			settings.setTextSize(WebSettings.TextSize.SMALLEST);
			break;
		case 2:
			settings.setTextSize(WebSettings.TextSize.SMALLER);
			break;
		case 3:
			settings.setTextSize(WebSettings.TextSize.NORMAL);
			break;
		case 4:
			settings.setTextSize(WebSettings.TextSize.LARGER);
			break;
		case 5:
			settings.setTextSize(WebSettings.TextSize.LARGEST);
			break;
		}
	}

	@SuppressLint("NewApi")
	private static void setZoom(WebSettings settings, int fontSize) {

		switch (fontSize) {
		case 1:
			settings.setTextZoom(50);
			break;
		case 2:
			settings.setTextZoom(75);
			break;
		case 3:
			settings.setTextZoom(100);
			break;
		case 4:
			settings.setTextZoom(125);
			break;
		case 5:
			settings.setTextZoom(150);
			break;
		}
	}

}
